//
//	 ______    ______    ______    
//	/\  __ \  /\  ___\  /\  ___\   
//	\ \  __<  \ \  __\_ \ \  __\_ 
//	 \ \_____\ \ \_____\ \ \_____\ 
//	  \/_____/  \/_____/  \/_____/ 
//
//	Powered by BeeFramework
//
//
//  PersonalHomePage.m
//  DingDing
//
//  Created by LHL on 14/11/27.
//  Copyright (c) 2014年 com.gordon. All rights reserved.
//

#import "PersonalHomePage.h"
#import "Personalshare.h"
#import "Common.h"
#import "PullLoader.h"
#import "FootLoader.h"
#import "MJRefresh.h"
#import "PersonalCell.h"
#import "Board_iPhone_Settings.h"
#import "ChatViewController.h"
#import "LargeImageViewController.h"
#pragma mark -

@interface PersonalHomePage()
{
    __weak IBOutlet UITableView *_tableview;
    PersonalCell *firstcell;
    UIButton *setting;
	//<#@private var#>
}
@end

@implementation PersonalHomePage

DEF_MODEL(getUserInfo, igetUserInfo)

- (void)load
{
   
}

- (void)unload
{
    
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}

ON_SIGNAL3(getUserInfo, RELOADED, SIGNAL){
    [_tableview headerEndRefreshing];
    [_tableview footerEndRefreshing];
    if ([self.igetUserInfo.code isEqualToString:@"200"]) {
        if ([self.igetUserInfo.data.user_id isEqualToString:[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"]]) {
            setting.hidden = NO;
        }else{
            setting.hidden = YES;
        }
        [_tableview reloadData];
    }else{
        [self presentMessageTips:self.igetUserInfo.message];
    }
    _tableview.footerHidden = !self.igetUserInfo.more;
}

ON_SIGNAL3(getUserInfo, RELOADING, SIGNAL){

}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self presentLoadingTips:@"加载中"];
    self.igetUserInfo = [getUserInfo modelWithObserver:self];
    self.igetUserInfo.fid = self.nearbymodel.user_id;
    [_tableview registerClass:[Personalshare class] forCellReuseIdentifier:@"cell"];
    
    [_tableview addHeaderWithCallback:^{
        [self.igetUserInfo firstPage];
    }];
    
    [_tableview addFooterWithCallback:^{
       [self.igetUserInfo nextPage];
    }];
    
    [self.igetUserInfo firstPage];
//    self.navigationBarLeft = [UIImage imageNamed:@"back.png"];
    self.navigationBarShown = NO;
    UIButton *leftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
    leftArrow.frame = CGRectMake(10, 50, 22, 22);
    [leftArrow setImage:[UIImage imageNamed:@"back.png"] forState:0];
    [leftArrow addTarget:self action:@selector(popFunc) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftArrow];
    
    setting =[UIButton buttonWithType:UIButtonTypeCustom];
    setting.frame = CGRectMake([UIScreen mainScreen].bounds.size.width-32, 50, 22, 22);
    [setting setImage:[UIImage imageNamed:@"setting.png"] forState:0];
    [setting addTarget:self action:@selector(settingFunc) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:setting];
    
}
ON_WILL_APPEAR( signal )
{
    self.navigationBarShown = NO;
}
-(void)popFunc{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)settingFunc{
    Board_iPhone_Settings *settingVC = [[Board_iPhone_Settings alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
}

#pragma mark - tableview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSLog(@"%u",self.igetUserInfo.data.item.count+1);
    if (self.igetUserInfo.data.petInfo == nil) {
        return 0;
    }else{
        return self.igetUserInfo.data.item.count+1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        firstcell = [tableView dequeueReusableCellWithIdentifier:@"firstcell"];
        [firstcell getPopBlock:^{
            [self dismissTips];
        }];
        if (self.igetUserInfo.data.item.count == 0) {
            [firstcell initcorner:self.igetUserInfo.data.petInfo hasline:NO];
        }else{
            [firstcell initcorner:self.igetUserInfo.data.petInfo hasline:YES];
        }
        [firstcell.msgButton addTarget:self action:@selector(startChat) forControlEvents:UIControlEventTouchUpInside];
        return firstcell;
        
    }else{
        Personalshare *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        [cell getClickImage:^(NSInteger num){
            SHAREITEMMODEL *model = self.igetUserInfo.data.item[indexPath.row-1];
            LargeImageViewController * large = [[LargeImageViewController alloc]init];
            [large setimageCount:model andCurrentIndex:num];
            [self.view addSubview:large];
            
        }];
        [cell rebuildFrame:self.igetUserInfo.data.item[indexPath.row-1]];
        return cell;
    }
}
-(void)startChat{
   ChatViewController* chatController = [[ChatViewController alloc] initWithChatter:self.igetUserInfo.data.IMusername isGroup:NO];
    
    chatController.usermodel = self.nearbymodel;
//    [conversation markMessagesAsRead:YES];
    [self.navigationController pushViewController:chatController animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 245;
    }else{
        return [Common getItemHeight:self.igetUserInfo.data.item[indexPath.row-1]]+10;
    }
}


ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.navigationController popViewControllerAnimated:YES];
}
ON_SIGNAL3(PersonalHomePageFirst, arrow, SIGNAL){
    [self.navigationController popViewControllerAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}

@end
